package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.R;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class ModifyNickNameFragment extends TitleBaseFragment {
    private EditText edt_name;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_modify_nickname, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        edt_name = view.findViewById(R.id.edt_name);

        setHeaderTitle("修改昵称");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("保存");
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UpdataName();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ModifyNickNameFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ModifyNickNameFragment");
    }


    private void UpdataName() {
        String s = edt_name.getText().toString();
        if (s.equals("")) {
            ToastUtil.showToast("请输入昵称");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("nickname", s);
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(handler, HttpCom.getUserURL(), map, false);
    }

    private MineManagerActivity activity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MineManagerActivity) activity;
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("修改昵称json", msg.obj.toString());
                    UserInfo userInfo = HttpUtils.DNSUser(msg.obj.toString());
                    if (userInfo != null) {
                        activity.popTopFragment(null);
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
}
